const md5 = require('md5');
const fs = require('fs');
const path = require('path');
const crtead = require('../lib/cread');

function index(router) {
	// 登录
	router.post('/login', async (ctx) => {
		// console.log(123123)
		var param = ctx.request.body;
		var is = await ctx.db.get({
			dbname: "mmuser",
			field: 'wechatNumber',
			where: {
				wechatNumber: param.wechatNumber
			}
		});
		var data = await ctx.db.get({
			dbname: "mmuser",
			field: "*",
			where: param
		});
		// console.log(data)
		ctx.response.body = {
			data: {
				success: data.length ? true : false,
				info: data.length ? '验证通过' : is.length?'用户名或密码不正确,请重新输入':'用户不存在',
				lst: data
			}
		};
	});
	//注册
	
	router.post('/registe', async (ctx) => {
		var param = ctx.request.body;
		console.log(param)
		
		var data = await ctx.db.get({
			dbname: "mmuser",
			field: 'wechatNumber',
			where: {
				wechatNumber: param.wechatNumber
			}
		});
		console.log(data)
		// return;
		if (!data.length) {
			param.crteadtime = Date.now();
			var ip = ctx.win.ip;
			if(ip.indexOf('http') == -1){
				ip = 'http://'+ ip;
			};
			param.headImg = ip +':8888/upload/log-c_05.png';
			// param.lanmuid = 0;
			var datas = await ctx.db.add({
				dbname: "mmuser",
				field: param
			});
			var news = await ctx.db.get({
				dbname: "mmuser",
				field: "*",
				where: {
					userName: param.userName
				}
			});
			// console.log(news[0])
			crtead(news[0].userId);
			var type = await ctx.db.query("CREATE TABLE "+news[0].userId+"name (id INT auto_increment primary key, list VARCHAR(2550), top VARCHAR(255), time VARCHAR(255), content VARCHAR(1000), img VARCHAR(255), video VARCHAR(255), city VARCHAR(255))");
			// console.log(type)
		};
		ctx.response.body = { 
			data: {
				success: !data.length ? true : false,
				info: !data.length ? '注册成功' : '用户名已存在!'
			}
		};
	});
	// 获取用户列表
	router.post('/flst', async (ctx) => {
		var param = ctx.request.body;
		var data = await ctx.db.get({
			dbname: "mmuser",
			field: "*"
		});
		ctx.response.body = {
			data: {
				success: true,
				info: 'ok',
				list: data
			}
		};
	});
	
}
module.exports = index;
